/****************************************************************************************************
* Title: Workhorses of Opportunity, Howard and Weinstein
*
* Inputs:
*   - justnormasylum.dta
*   - online_table4-2-1.dta
*
* Outputs:
*   - fracparentsdecile_90.pdf
*
* Description:
*   This script estimates the effect of being in a county with a normal school on the fraction of
*   parents in each national income decile. It constructs 95% and 90% confidence intervals and
*   overlays them on a graph with baseline averages from asylum counties.
****************************************************************************************************/


clear 
use "justnormasylum"
rename hasnormalschool hasnormal
keep hasnormal cty_fips 
tempfile normal
save `normal'

*Master do file
clear
clear matrix
clear mata
set maxvar 15000


clear
clear
use online_table4-2-1.dta
gen cty_fips=cty1990
drop county_name cz cz_name cz_pop2000 statename state_id stateabbrv

replace cty_fips=12086 if cty_fips==12025


gen state=floor(cty_fips/1000)

merge 1:m cty_fips using `normal', keep(3)




forvalues i = 1(1)10{
gen est`i' = .
gen lb`i' = .
gen ub`i' = .
gen lb90_`i' = .
gen ub90_`i' = .
gen meanasylum`i' = . 

#delimit ;
local vars frac_`i';

foreach z of local vars{;
di "`z'";

reghdfe `z' hasnormal  , absorb(state) cluster(state);
#delimit cr
 mat A = e(b)
 mat B = e(V)
gen df = e(df_r)
gen se = sqrt(B[1,1])
replace est`i' = A[1,1]
replace lb`i' = est`i' - (invt(df,.975)*se)
replace ub`i' = est`i' + (invt(df,.975)*se)
replace lb90_`i' = est`i' - (invt(df,.95)*se)
replace ub90_`i' = est`i' + (invt(df,.95)*se)
su est`i' lb`i' ub`i'
matrix drop A B 
drop df se
su `z' if hasnormal==0 & e(sample)==1
replace meanasylum`i' = r(mean)
}
}

gen count = _n
#delimit ;
keep est1 est2 est3 est4 est5 est6 est7 est8 est9 est10 lb1 lb2 lb3 lb4 lb5 lb6 lb7 lb8 lb9 lb10 lb90* ub90*
ub1 ub2 ub3 ub4 ub5 ub6 ub7 ub8 ub9 ub10 meanasylum1 meanasylum2 meanasylum3 meanasylum4 
meanasylum5 meanasylum6 meanasylum7 meanasylum8 meanasylum9 meanasylum10 count;

#delimit cr
keep if count==1
reshape long est lb ub lb90_ ub90_ meanasylum, i(count) j(decile)

#delimit ;
twoway(scatter est decile, mcolor(dkgreen)) (rspike lb ub decile, color(dkgreen)) (rcap lb90 ub90 decile, color(dkgreen))
(line meanasylum decile, connect(l) yaxis(2) lpattern(dash) lcolor(orange_red)), yline(0, lpattern(solid) lcolor(gs8)) yline(.1, lpattern(dash) lcolor(gs12) axis(2)) 
legend(off) xlabel(1 2 3 4 5 6 7 8 9 10) graphregion(fcolor(white)) xtitle("National Income Decile") text(.102 7 "10% in National Decile", size(small) color(gs8) yaxis(2) place(e))
ytitle("Effect of Normal School on Frac. Parents in Decile (Solid)", axis(1)) ysc(r(-.05 .05) axis(1)) ylabel(-.05(.02).05, axis(1)) ytitle("Frac. Parents in Decile in Asylum Counties (Dashed)", axis(2))
;
//graph save fracparentsdecile.gph, replace;
graph export "fracparentsdecile_90.pdf", replace;



#delimit cr
